/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.helper;

import android.content.Context;
import android.graphics.*;
import android.os.Handler;
import android.view.*;
import android.widget.*;

import com.feiyue.basic.server.cache.CacheFetch;
import com.feiyue.basic.server.cache.DiskLruCache;
import com.feiyue.basic.server.db.BookCollectDBHandler;
import com.feiyue.basic.server.parser.Book;

import java.io.File;
import java.util.List;

public class BookListAdapter extends SimpleAdapter
{
	private class MyThread extends Thread
	{

		private Context context;
		private String coverAddress;
		private Handler handler;
		private int height;
		private Myrunnable myrunnable;
		private int width;

		public void run()
		{
			try {
				File file = CacheFetch.getFile(context, coverAddress, coverAddress, false);
				if (file != null) {
					Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
					if (bitmap != null)
					{
						int i = bitmap.getWidth();
						int j = bitmap.getHeight();
						float f = (float)((double)width / (double)i);
						float f1 = (float)((double)height / (double)j);
						Matrix matrix = new Matrix();
						matrix.postScale(f, f1);
						Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true);
						myrunnable.setBitmap(bitmap1);
						handler.post(myrunnable);
					}
				}
			} catch (OutOfMemoryError e) {
				System.gc();
			}
		}

		public MyThread(Context context1, Myrunnable myrunnable1, Handler handler1, String s, int i, int j)
		{
			super();
			context = context1;
			myrunnable = myrunnable1;
			handler = handler1;
			coverAddress = s;
			width = i;
			height = j;
		}
	}

	private class Myrunnable
		implements Runnable
	{

		private Bitmap bitmap;
		private ImageView coverImageView;

		public void run()
		{
			coverImageView.setImageBitmap(bitmap);
		}

		public void setBitmap(Bitmap bitmap1)
		{
			bitmap = bitmap1;
		}

		public Myrunnable(ImageView imageview)
		{
			super();
			coverImageView = null;
			bitmap = null;
			coverImageView = imageview;
		}
	}


	private List booklist;
	private List collectbooklist;
	private int height;
	private LayoutInflater inflater;
	private Context mContext;
	private int resource;
	private int width;

	public BookListAdapter(Context context, List list, int i, String as[], int ai[], List list1)
	{
		super(context, list, i, as, ai);
		mContext = context;
		inflater = (LayoutInflater)context.getSystemService("layout_inflater");
		resource = i;
		booklist = list1;
		width = BitmapFactory.decodeResource(mContext.getResources(), 0x7f0200b1).getWidth();
		height = BitmapFactory.decodeResource(mContext.getResources(), 0x7f0200b1).getHeight();
		BookCollectDBHandler bookcollectdbhandler;
		try {
			bookcollectdbhandler = new BookCollectDBHandler(context);
			collectbooklist = bookcollectdbhandler.getBookList(context);
			bookcollectdbhandler.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private boolean alreadycollect(int i)
	{
		int j;
		int k;
		boolean flag = false;
		if (collectbooklist != null && collectbooklist.size() > 0) {
			j = collectbooklist.size();
			k = 0;
			
			while (k < j) {
				if (i == ((Book)collectbooklist.get(k)).getId()) {
					flag = true;
					break;
				}
			}
		}
		
		return flag;
	}

	public View getView(final int position, View view, ViewGroup viewgroup)
	{
		ImageView imageview;
		Bitmap bitmap;
		if (view == null)
			view = inflater.inflate(resource, viewgroup, false);
		((LinearLayout)view.findViewById(0x7f06001b)).setVisibility(0);
		boolean flag = alreadycollect(((Book)booklist.get(position)).getId());
		final ImageView btn = (ImageView)view.findViewById(0x7f060032);
		File file;
		DiskLruCache disklrucache;
		File file1;
		Bitmap bitmap1;
		if (flag)
			btn.setImageResource(0x7f020027);
		else
			btn.setImageResource(0x7f020000);
		btn.setVisibility(0);
		btn.setOnClickListener(new android.view.View.OnClickListener() {

			public void onClick(View view1)
			{
				try
				{
					BookCollectDBHandler bookcollectdbhandler = new BookCollectDBHandler(mContext);
					collectbooklist = bookcollectdbhandler.getBookList(mContext);
					btn.setImageResource(0x7f020027);
					if (!bookcollectdbhandler.isExist((Book)booklist.get(position)))
					{
						bookcollectdbhandler.insert((Book)booklist.get(position), mContext);
						Toast.makeText(mContext, "收藏成功", 0).show();
						Context context = mContext;
						Object aobj[] = new Object[1];
						aobj[0] = Integer.valueOf(((Book)booklist.get(position)).getId());
						(new Thread2SendFavoriteReport(context, String.format("http://book.yy567.net/bookCollection?type=collect&bookid=%s", aobj))).start();
					} else
					{
						Toast.makeText(mContext, "已收藏", 0).show();
					}
					bookcollectdbhandler.destroy();
				}
				catch (Exception exception) { }
			}
		});
		((TextView)view.findViewById(0x7f06002f)).setText(((Book)booklist.get(position)).getName());
		((TextView)view.findViewById(0x7f060030)).setText((new StringBuilder("作者：")).append(((Book)booklist.get(position)).getAuthor()).toString());
		((TextView)view.findViewById(0x7f060031)).setText((new StringBuilder("状态：")).append(((Book)booklist.get(position)).getIssuestate()).toString());
		imageview = (ImageView)view.findViewById(0x7f06002d);
		imageview.setImageResource(((Book)booklist.get(position)).getCoverid(Boolean.valueOf(true)));
		if (((Book)booklist.get(position)).getCoverAddr() != null && !((Book)booklist.get(position)).getCoverAddr().equals("null")) {
			file = DiskLruCache.getDiskCacheDir(mContext, "http");
			disklrucache = DiskLruCache.openCache_get(mContext, file, 0xa00000L);
			if (disklrucache != null) {
				file1 = new File(disklrucache.createFilePath(((Book)booklist.get(position)).getCoverAddr()));
				if (!disklrucache.containsKey(((Book)booklist.get(position)).getCoverAddr())) {
					Myrunnable myrunnable = new Myrunnable(imageview);
					Handler handler = new Handler();
					(new MyThread(mContext, myrunnable, handler, ((Book)booklist.get(position)).getCoverAddr(), width, height)).start();
				} else {
					try {
						bitmap = BitmapFactory.decodeFile(file1.getPath());
						
						if (bitmap != null)
						{
							int i = bitmap.getWidth();
							int j = bitmap.getHeight();
							float f = (float)((double)width / (double)i);
							float f1 = (float)((double)height / (double)j);
							Matrix matrix = new Matrix();
							matrix.postScale(f, f1);
							imageview.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true));
						}
					} catch (OutOfMemoryError e) {
						System.gc();
					}
					
				}
			}
		}
		
		return view;
	}

	public void setCollectbooklist(List list)
	{
		collectbooklist = list;
	}
}